package com.plant.action;

import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.plant.entity.UgcTopicJoin;
import com.plant.entity.UgcUploadImages;
import com.plant.service.NewThingsService;
import com.plant.tool.BuildString;

/**
 *	@author JAVA工程师--李琦
 *
 */
@Controller
@RequestMapping(value="/ugctopicjoin")
public class UgcTopicjoinAction {
	
	@Resource(name = "newThingsServiceImpl")
    private NewThingsService newThingsService;
	
	
	@RequestMapping(value="topicjoin")//1---话题参与讨论
	@ResponseBody
	public Map<Object,Object> newthings(HttpServletResponse response,Integer to_topic_id,Integer user_id,@RequestParam(value="file" ,required=false) MultipartFile[] myfiles,UgcTopicJoin utj,String secret,String pust_val,HttpServletRequest request,@RequestParam(value="remind_who[]",required=false) String[] ids){
		response.setHeader("access-control-allow-origin","*");
		Map<Object,Object> map=new HashMap<Object, Object>();
		if(secret==null||pust_val==null||user_id==null||to_topic_id==null){
			map.put("message", "参数为空");
			map.put("state", 0);
			return map;
		}else{
			Md md=new Md();
			String mdsc=md.md5calc(pust_val+"flower_duigu2016");
			if(secret.endsWith(mdsc)){
				System.out.println("提醒谁看");
				if(ids==null){
					System.out.println("提心谁看为空");
				}else{
					for (int i = 0; i < ids.length; i++) {
						if(ids[i]==null){
							utj.setRemindUserId("");
						}else{
							BuildString build=new BuildString();
							String remind_user_id=build.StringBuilder(ids);//处理艾特谁看的id
							utj.setRemindUserId(remind_user_id);
					    }
					 }
				}
				SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			   	String t=sim.format(new Date()); 
				GetIp getip=new GetIp();
			   	String ip=getip.getIp2(request);
			   	utj.setIp(ip);
			   	utj.setCreateTime(Timestamp.valueOf(t));
			   	utj.setToTopicId(to_topic_id);
			   	utj.setUserId(user_id);
				boolean b=newThingsService.AddTopicJoin(utj);
				if(myfiles.length==0){
					System.out.println("传过来的图片为空");
				}else{
				for(MultipartFile myfile : myfiles){  
			            if(myfile.isEmpty()){  
			                System.out.println("");
			            }else{  
			                System.out.println("图片尺寸: " + myfile.getSize());  
			                System.out.println("图片类型: " + myfile.getContentType());  
			                System.out.println("图片名称: " + myfile.getName());  
			                System.out.println("图片原始名称: " + myfile.getOriginalFilename());  
			                System.out.println("========================================");  
			                String fileName=myfile.getOriginalFilename();
			                fileName = fileName.substring(fileName.lastIndexOf("\\")+1);
			                String extName = fileName.substring(fileName.lastIndexOf("."));//.jpg
			                String uuid = UUID.randomUUID().toString().replace("-", "");
			                String newName = uuid+extName;   
			                String realPath=request.getSession().getServletContext().getInitParameter("UPLOAD_IMAGE_PATH_TOPICJOIN");
			                try {
								org.apache.commons.io.FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(realPath,newName));
							} catch (IOException e) {
								e.printStackTrace();
							}  
							String url="http://www.zhiwuxiansheng.com:8080/images/topicjoin/"+newName;
							UgcUploadImages upi=new UgcUploadImages();
							upi.setCreateTime(Timestamp.valueOf(t));
							upi.setIp(ip);
							upi.setFromContentType(4);
							upi.setUserId(utj.getUserId());
							upi.setFromContentId(utj.getId());
							upi.setUrl(url);
							newThingsService.addnewthingsurl(upi); //插入数据库
			            }  
			        }  
				}
				if(b){
					map.put("state", 1);
					map.put("message", "参与讨论成功");
					return map;
				}else{
					map.put("state", 0);
					map.put("message", "参与讨论失败");
					return map;
				}
			}else{
				map.put("message", "秘钥错误");
				map.put("state", 0);
				return map;	
			}
		}
	}
	
	
	
	
}
